/* 
 * Solr with RankingAlgorithm 
 * --------------------------   
 * SolrJ Test Code to test adding documents and querying them in NRT without a commit 
 * 
 * Make the following changes to your schema.xml: 
 * 
 *	<fields>  
 *	   <field name="id" type="string" required="true" />
 *	   <field name="name" type="string" required="true" />
 *	   <field name="price" type="float" required="true" />
 *	</fields>
 * 
 * Make sure the following are in solrconfig.xml: 
 * 
 * <realtime visible="200">true</realtime>
 *  <library>rankingalgorithm</library>
 *  <rankingalgorithm>
 *	 <algorithm>simple</algorithm>
 *	</rankingalgorithm>
 * 
 * Code donated by: 
 * Sharmistha Chakraborty 
 *	Reachable @ mona.a.in@gmail.com 
 * 
 * Version: 
 * 1.1 - modifications for Solr 4.0 Alpha 
 * 
 * Date: 
 * 2012-07-18   
 */ 
package org.apache.solr.client.solrj.util; 

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;


public class SolrjConcurrentUpdateQueryTest { 
        static public void main(String args[]) {	
                        try{ 
                                ConcurrentUpdateSolrServer server; 
                                server = new ConcurrentUpdateSolrServer("http://localhost:8983/solr/collection1", 10, 5); 
                                int docloopEnd = 100; 
                                int docloopStart = 0; 
                                
                                for(int loop=0; loop<100; loop++){ 
                                        /* Add 100 documents */ 
                                        long start1 = System.currentTimeMillis(); 
                                        for(int i=docloopStart; i<docloopEnd; i++){ 
                            addDocument(server, i); 
                                        } 
                                        docloopStart = docloopEnd; 
                                        docloopEnd += 100; 
                                        long end1 = System.currentTimeMillis(); 
                    long totTimeAdd = end1 - start1; 

                    System.out.println("Total time in adding 100 docs (" 
                                                    + totTimeAdd + " ms)"); 
                                        /* Now wait for about 10 secs, delay is more to see the messages scrolling on the screen */ 
                    
//                    Thread.sleep(1000); 
                
                    /* Now query Solr with RankingAlgorithm to see how many docs got added */ 
                        long start2 = System.currentTimeMillis(); 
                        searchDoc(server, "*:*"); 
                        long end2 = System.currentTimeMillis(); 
                        long totSearchTime = end2 - start2; 
                        System.out.println("Total time for searching *:* (" 
                                                        + totSearchTime + " ms)"); 
                        
                    /* Now wait for about 10 secs, delay is more to see the messages scrolling on the screen */ 
                        
                    Thread.sleep(1000); 
                    
                    /* Now query Solr with RankingAlgorithm to find a particular doc */ 
                        start2 = System.currentTimeMillis(); 
                        searchDoc(server, "doc"+loop); 
                        end2 = System.currentTimeMillis(); 
                        totSearchTime = end2 - start2; 
                        System.out.println("Total time for searching doc" + loop + " (" 
                                                        + totSearchTime + " ms)"); 
                                } 
                        }  catch (Exception e) { 
                                e.printStackTrace(); 
                        } 
        } 
        
   public static void addDocument( SolrServer solr, int i )  { 
       try { 

           SolrInputDocument solrDoc = new SolrInputDocument(); 
           solrDoc.addField("id", "id"+i); 
           solrDoc.addField("cat", "1"); 
           solrDoc.addField("price", i); 
           solr.add(solrDoc); 
       } catch (SolrServerException e) { 
      e.printStackTrace(); 
       } catch (Exception e) { 
      e.printStackTrace(); 
       } 
   } 

   private static void searchDoc(SolrServer solr, String queryStr)  { 
       SolrQuery solrQ = new SolrQuery(); 
       solrQ.setQuery(queryStr); 
       QueryResponse qr; 

       try { 
      qr = solr.query(solrQ); 
      SolrDocumentList sdl = qr.getResults(); 
      System.out.println("Searching for:" + queryStr + ", found: " + sdl.getNumFound() +" docs"); 
       } catch (SolrServerException e) { 
      e.printStackTrace(); 
       } catch (Exception e) { 
      e.printStackTrace(); 
       } 
   } 
}